﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HallTimer.Classes;
using HallTimer.Interfaces;
using System.ComponentModel;

namespace HallTimer.Views.Subwindows
{
    /// <summary>
    /// Interaction logic for TimerPage.xaml
    /// </summary>
    public partial class TimerPage : Page, ITimerCallback 
    {
        private CountDownTimer timer;
        private int currentSelectedValue;

        public TimerPage()
        {
            InitializeComponent();
            currentSelectedValue = 0;
        }

        public void startTimer()
        {
            //timer = new CountDownTimer(this, 5);
            // search selected value
            // start timer
            timer = new CountDownTimer(this, currentSelectedValue);

            uncheckButtons();

            adjustedTimeRadioButton.IsChecked = false;
            minutesStackPanel.IsEnabled = false;
            program1StackPanel.IsEnabled = false;
            program2StackPanel.IsEnabled = false;
            zondagStackPanel.IsEnabled = false;
            adjustedTimeStackPanel.IsEnabled = false;
        }

        public void stopTimer()
        {
            timer.StopTimer();
            resterendTempLabel.Content = "Resterend: ";
            minutesStackPanel.IsEnabled = true;
            program1StackPanel.IsEnabled = true;
            program2StackPanel.IsEnabled = true;
            zondagStackPanel.IsEnabled = true;
            adjustedTimeStackPanel.IsEnabled = true;
        }

        /// <summary>
        /// Callback method for the countdown timer
        /// </summary>
        /// <param name="timeLeft"></param>
        public void showTimerValue(TimeSpan timeLeft)
        {
            //timer.StopTimer();
            //MessageBox.Show("Timer: " + timeLeft.ToString());
            //testTimerLabel.Content = "Timer: " + timeLeft.ToString();

            // when zero... stop the timer...
            if ((timeLeft.Minutes > 0) || (timeLeft.Seconds > 0))
            {
                MainWindow.mainWindowRef.SetTimerValue(timeLeft.Minutes, timeLeft.Seconds);
            }
            else
            {
                MainWindow.mainWindowRef.StoppingTimer(this);
                minutesStackPanel.IsEnabled = true;
                program1StackPanel.IsEnabled = true;
                program2StackPanel.IsEnabled = true;
                zondagStackPanel.IsEnabled = true;
                adjustedTimeStackPanel.IsEnabled = true;
            }
            resterendTempLabel.Content = "Resterend: " + timeLeft.ToString();
        }

        // Executed when switched to other tab (and thus other page)...
        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            // TODO: decouple timer object and callback... display black window...
            // or: stay timing, until other action is executed...
            //MessageBox.Show("Test");
        }

        private void timeRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            MainWindow.mainWindowRef.ActivateTimerStartButton();
            RadioButton button = (RadioButton)sender;
            int tagValue = 0;
            if (int.TryParse(button.Tag.ToString(), out tagValue))
            {
                currentSelectedValue = tagValue;
            }            
        }

        private void adjustedTimeRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            timeRadioButton_Checked(sender, e);
            timeTextBox.IsEnabled = true;
            String timerText = timeTextBox.Text;
            if (timerText.Length > 0) // if there was a value previously filled in...
            {
                setCurrentValue(timerText);
            }
            timeTextBox.Focus();
        }

        private void adjustedTimeRadioButton_Unchecked(object sender, RoutedEventArgs e)
        {
            timeTextBox.IsEnabled = false;
        }

        /// <summary>
        /// Method designed to prevent other characters in the timer textbox (only numeric values allowed)
        /// (max number of characters is set as a property on the TextBox)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timeTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            int value = 0;
            if (int.TryParse(e.Text, out value)) // check if numeric
            {
                if (((value == 0) && (timeTextBox.Text.Length == 0)) // first number shouldn't be a zero
                    || ((value < 0) || (value > 9))) // to be absolutely sure there will be never a "wrong" value
                {
                    e.Handled = true;
                }
            }
            else // (not numeric)
            {
                e.Handled = true;
            }
        }

        private void timeTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            setCurrentValue(((TextBox)sender).Text);
        }

        private void setCurrentValue(String textBoxMinutes)
        {
            // TODO: input check
            int minutes = 0;
            if (int.TryParse(textBoxMinutes, out minutes))
            {
                currentSelectedValue = minutes;
            }
        }

        private void uncheckButtons()
        {
            t3MinutesRadioButton.IsChecked = false;
            t5MinutesRadioButton.IsChecked = false;
            t10MinutesRadioButton.IsChecked = false;
            t15MinutesRadioButton.IsChecked = false;
            t20MinutesRadioButton.IsChecked = false;
            t25MinutesRadioButton.IsChecked = false;
            t30MinutesRadioButton.IsChecked = false;

            gbsRadioButton.IsChecked = false;
            blgPart1RadioButton.IsChecked = false;
            blgPart2RadioButton.IsChecked = false;

            voorlezenRadioButton.IsChecked = false;
            tbsNr2RadioButton.IsChecked = false;
            tbsNr3RadioButton.IsChecked = false;

            lectureRadioButton.IsChecked = false;

            adjustedTimeRadioButton.IsChecked = false;
            timeTextBox.Text = "";
        }
    }
}
